package com.jwgf.smarowindingmachine_unity.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.jwgf.common.ScreenOffTimeoutControl;
import com.jwgf.common.ViewCache;
import com.jwgf.smarowindingmachine_unity.R;
import com.jwgf.smarowindingmachine_unity.logic.MachineParameterSharedPreference;


public class EquipmentMaintenanceDeviceSettingFragment extends BaseFragment {
    public static final String fragmentTag = "EquipmentMaintenanceDeviceSettingFragment";
    private ImageButton btn_screen_off_display;
    private TextView screen_off_display_value;

    private ImageButton btn_cbf_alarm;
    private TextView cbfText;

    private ImageButton btn_bad_spindle;
    private TextView badSpindleText;

    private EditText screen_off_timeout_edit;

    MachineParameterSharedPreference sp  = MachineParameterSharedPreference.getInstance();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = ViewCache.getInstance().getView(R.layout.fragment_equipment_maintenance_device_setting);
        screen_off_display_value = v.findViewById(R.id.screen_off_display_value);
        String[] content = getResources().getStringArray(R.array.yes_no_choose);
        screen_off_display_value.setText(content[ScreenOffTimeoutControl.getScreenOffDisplay()]);

        btn_screen_off_display = v.findViewById(R.id.btn_screen_off_display);
        btn_screen_off_display.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String[] content = getResources().getStringArray(R.array.yes_no_choose);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        screen_off_display_value.setText(content[position]);
                        ScreenOffTimeoutControl.setScreenOffDisplay(position);
                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");
            }
        });

        cbfText = v.findViewById(R.id.cbf_alarm_value);
        String[] cbfContent = getResources().getStringArray(R.array.yes_no_choose);
        int isYes = Integer.parseInt(sp.getValue(MachineParameterSharedPreference.CBF_ALARM, "0"));
        cbfText.setText(cbfContent[isYes]);

        btn_cbf_alarm = v.findViewById(R.id.btn_cbf_alarm);
        btn_cbf_alarm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String[] content = getResources().getStringArray(R.array.yes_no_choose);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        cbfText.setText(content[position]);
                        sp.setValue(MachineParameterSharedPreference.CBF_ALARM, String.valueOf(position));
                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");
            }
        });

        badSpindleText = v.findViewById(R.id.bad_spindle_value);
        String[] badContent = getResources().getStringArray(R.array.yes_no_choose);
        int isYesBad = Integer.parseInt(sp.getValue(MachineParameterSharedPreference.BAD_SPINDLE, "0"));
        badSpindleText.setText(badContent[isYesBad]);

        btn_bad_spindle = v.findViewById(R.id.btn_bad_spindle);
        btn_bad_spindle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String[] content = getResources().getStringArray(R.array.yes_no_choose);
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        badSpindleText.setText(content[position]);
                        sp.setValue(MachineParameterSharedPreference.BAD_SPINDLE, String.valueOf(position));
                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");
            }
        });

        screen_off_timeout_edit = v.findViewById(R.id.screen_off_value);
        screen_off_timeout_edit.setText(String.valueOf(ScreenOffTimeoutControl.getTimeout() / 1000));
        screen_off_timeout_edit
                .setOnEditorActionListener(new OnEditorActionListener() {

                    @Override
                    public boolean onEditorAction(TextView v, int actionId,
                                                  KeyEvent event) {
                        if (!TextUtils.isEmpty(v.getText().toString())) {
                            int timeout = Integer.valueOf(v.getText()
                                    .toString());
                            if (timeout >= 60) {
                                ScreenOffTimeoutControl.setTimeout(
                                        timeout * 1000);
                            } else {
                                v.setText(String.valueOf(ScreenOffTimeoutControl.getTimeout() / 1000));
                            }
                        } else {
                            v.setText(String.valueOf(ScreenOffTimeoutControl.getTimeout() / 1000));
                        }

                        return false;
                    }
                });
        return v;
    }

}
